<html><!-- #BeginTemplate "/Templates/maintemplate.dwt" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- #BeginEditable "doctitle" --> 
<title>JEP - Java Math Expression Parser</title>
<!-- #EndEditable --> 
<link rel="stylesheet" type="text/css" href="main.css" title="style1">
</head>

<body>



<!-- NAVIGATION ---------------------------------------------------- -->
<div id="navcontainer"> 
  <ul>
    <li><a href="../javadoc/index.html" target="_blank">JavaDoc <img src="img/new-window-icon.gif" width="15" height="11"></a></li>
  </ul>
  <h1>JEP</h1>
  <ul>
    <li><a href="index.html">Basic Usage</a></li>
    <li><a href="variables.html">Variables</a></li>
    <li><a href="types.html">Data types</a></li>
    <li><a href="operators.html">Operators</a></li>
    <li><a href="functions.html">Functions</a></li>
    <li><a href="advanced.html">Advanced Features</a></li>
    <li><a href="grammar.html">Grammar</a></li>
    <li><a href="faq.html">FAQ</a></li>
    <li><a href="version.html">Version History</a></li>
  </ul>
  <h1>Extensions</h1>
  <ul>
    <li><a href="extensions/index.html">Overview</a></li>
    <li><a href="extensions/xjep.html">XJep</a></li>
    <li><a href="extensions/djep.html">Differentiation</a></li>
    <li><a href="extensions/vectorjep.html">Vectors and Matrices</a></li>
    <li><a href="extensions/groupjep.html">Groups</a></li>
    <li><a href="extensions/version.html">Version History</a></li>
  </ul>
</div>



<div id="centercontent">
<!-- CENTER CONTENT -------------------------------------------------- -->
<!-- #BeginEditable "Text" --> 
  <h1><a name="top"></a>Functions</h1>
  <p>Note that you can always add new <a href="#custom">custom functions</a>. 
    Each of the following functions can be applied to objects of the types indicated. 
  </p>
  Functions added with <b>addStandardFunctions()</b>: 
  <table border="0" cellspacing="1" cellpadding="2" width="100%" bgcolor="#CCCCCC">
    <tr bgcolor="#FFFFFF"> 
      <td width="225">&nbsp;</td>
      <td width="91">&nbsp;</td>
      <td width="76"><b>Double</b></td>
      <td width="81"><b>Complex</b></td>
      <td width="91"><b>String</b></td>
      <td width="91"><b>Vector</b></td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Sine</td>
      <td width="91">sin(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Cosine</td>
      <td width="91">cos(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Tangent</td>
      <td width="91">tan(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Arc Sine</td>
      <td width="91">asin(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Arc Cosine</td>
      <td width="91">acos(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Arc Tangent</td>
      <td width="91">atan(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Arc Tangent (with 2 parameters)</td>
      <td width="91">atan2(y, x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Hyperbolic Sine</td>
      <td width="91">sinh(x)</td>
      <td width="76" bordercolor="#CCCCCC" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" bordercolor="#CCCCCC" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" bordercolor="#CCCCCC" align="center">&nbsp;</td>
      <td width="91" bordercolor="#CCCCCC" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Hyperbolic Cosine</td>
      <td width="91">cosh(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Hyperbolic Tangent</td>
      <td width="91">tanh(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Inverse Hyperbolic Sine</td>
      <td width="91">asinh(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Inverse Hyperbolic Cosine</td>
      <td width="91">acosh(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Inverse Hyperbolic Tangent</td>
      <td width="91">atanh(x)</td>
      <td width="76" bordercolor="#CCCCCC" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Natural Logarithm</td>
      <td width="91">ln(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Logarithm base 10</td>
      <td width="91">log(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Exponential (e^x)</td>
      <td width="91">exp(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Absolute Value / Magnitude</td>
      <td width="91">abs(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Random number (between 0 and 1)</td>
      <td width="91">rand()</td>
      <td width="76" align="center">&nbsp;</td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Modulus</td>
      <td width="91">mod(x,y) = x % y</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Square Root</td>
      <td width="91">sqrt(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Sum</td>
      <td width="91">sum(x,y,z)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">If</td>
      <td width="91">if(cond,trueval,falseval)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Str (number to string)</td>
      <td width="91">str(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Binomial coefficients</td>
      <td width="91">binom(n,i)</td>
      <td width="76" align="center">Integer values</td>
      <td width="81" align="center"></td>
      <td width="91" align="center"></td>
      <td width="91" align="center"></td>
    </tr>
  </table>
  <br>
  Functions added with <b>addComplex()</b>: 
  <table border="0" cellspacing="1" cellpadding="2" width="100%" bgcolor="#CCCCCC">
    <tr bgcolor="#FFFFFF"> 
      <td width="225">&nbsp;</td>
      <td width="91">&nbsp;</td>
      <td width="76"><b>Double</b></td>
      <td width="81"><b>Complex</b></td>
      <td width="91"><b>String</b></td>
      <td width="91"><b>Vector</b></td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Real Component</td>
      <td width="91">re(c)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Imaginary Component</td>
      <td width="91">im(c)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Complex Modulus (Absolute Value)</td>
      <td width="91">cmod(c)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Argument (Angle of complex value, in radians)</td>
      <td width="91">arg(c)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Complex conjugate</td>
      <td width="91">conj(c)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Complex, constructs a complex number from real and imaginar 
        parts</td>
      <td width="91">complex(x,y)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Polar, constructs a complex number from modulus and argument</td>
      <td width="91">polar(r,theta)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
  </table>
  <!-- #BeginLibraryItem "/Library/top bar.lbi" -->
<div class="topbar"><a href="#top"><img src="img/top.gif" width="38" height="15" name="top"></a></div>
<!-- #EndLibraryItem --> 
  <h2><a name="custom"></a>Custom Functions</h2>
  <p>The following is an example of how a custom function can be added.</p>
  <p>Assume you want to add a function &quot;half&quot; to divide a number by 
    two (for demonstration purposes).</p>
  <ol>
    <li>Create a class that extends PostfixMathCommand (in the org.nfunk.jep.function 
      package). In this example we will name it &quot;Half&quot;</li>
    <li>In the constructor set the number of arguments to be taken. In our case 
      this is one. Do this by writing &quot;numberOfParameters = 1;&quot;<br>
      If you want to allow any number of parameters, initialize the numberOfParameters 
      value to -1. It is highly recommended to study the Sum.java code as an example 
      of a function that accepts any number of parameters.</li>
    <li> Implement the run(Stack inStack) method. The parameters are passed in 
      a Stack object. This same stack is used as the output for the function. 
      So we first need to pop the parameter off the stack, calculate the result, 
      and finally pop the result back on the stack. 
      <pre>public void run(Stack inStack) throws ParseException {

   <font color="#999999">// check the stack</font>
   checkStack(inStack);
   
   <font color="#999999">// get the parameter from the stack</font>
   Object param = inStack.pop();

   <font color="#999999">// check whether the argument is of the right type</font><br>   if (param instanceof Double) {
      <font color="#999999">// calculate the result</font><br>      double r = ((Double)param).doubleValue() / 2;
      <font color="#999999">// push the result on the inStack</font><br>      inStack.push(new Double(r)); <br>   } else {<br>      throw new ParseException(&quot;Invalid parameter type&quot;);
   }
}</pre>
    </li>
    <li>In your main program or applet that is using the parser, add the new function. 
      Do this by writing<br>
      <pre>parser.addFunction("half", new Half());</pre>
    </li>
    <li>If numberOfParameters == -1 you may wish to overwrite the <tt>boolean 
      checkNumberOfParameters(int n)</tt> to catch an illegal number of arguments 
      at parse time.</li>
  </ol>
  <p><b>Source files</b></p>
  <ul>
    <li><a href="CustFunc.java">CustFunc.java</a> (a testing class)</li>
    <li><a href="Half.java">Half.java</a> (the Half function class)</li>
  </ul>
  <!-- #EndEditable -->




<!-- FOOTER ---------------------------------------------------------- -->
<div id="footer">
<a href="http://sourceforge.net/tracker/?func=add&group_id=24711&atid=382402">Report bugs / documentation errors</a><br/>
<br/>
&copy; 2006 <a href="http://www.singularsys.com" target="_blank">Singular Systems</a>
</div>
</div> <!-- centercontent -->

</body>
<!-- #EndTemplate --></html>
